package com.hzcx.app.simplechat.ui.group.adapter;

import com.github.promeg.pinyinhelper.Pinyin;
import com.hzcx.app.simplechat.ui.friend.bean.FriendBean;
import com.hzcx.app.simplechat.ui.friend.bean.FriendCityBean;
import com.hzcx.app.simplechat.ui.group.bean.MembersBean;
import com.hzcx.app.simplechat.util.PinyinUtil;
import com.hzcx.app.simplechat.util.UserInfoUtil;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;

import java.util.ArrayList;
import java.util.List;

public class GroupMembersSort {

    static public List<FriendBean> sortFriend(List<MembersBean> membersBeans) {

        List<FriendCityBean> data = new ArrayList<>();
        for (MembersBean membersBean : membersBeans) {
            if (membersBean.getMember_id() == UserInfoUtil.getUserId()) {
                continue;
            }
            data.add(FriendCityBean.convert(membersBean));
        }

        // 先设置每个用户的拼音，然后根据拼音排序
        for (FriendCityBean bean : data) {
            String pinyin = Pinyin.toPinyin(EmptyUtils.isNotEmpty(bean.getRemarks_name()) ? bean.getRemarks_name() : bean.getNickname(), "");
            bean.setPinyin(pinyin.toUpperCase());
        }
        // 排序
        PinyinUtil.transferList(data);
        // 排序成功后，整合数据
        // 先添加字幕列表数据
        List<FriendBean> list = new ArrayList<>();
        for (FriendCityBean bean : data) {
            boolean isAdd = true;
            for (FriendBean subBean : list) {
                if (subBean.getName().equals(PinyinUtil.isPinyin(bean.getPinyin().charAt(0)))) {
                    isAdd = false;
                    break;
                }
            }
            if (isAdd) {
                FriendBean friendBean = new FriendBean();
                friendBean.setName(PinyinUtil.isPinyin(bean.getPinyin().charAt(0)));
                List<FriendCityBean> cityBeanList = new ArrayList<>();
                friendBean.setCity(cityBeanList);
                list.add(friendBean);
            }
        }
        // 在添加好友数据
        for (FriendCityBean cityBean : data) {
            for (int i = 0; i < list.size(); i++) {
                if (PinyinUtil.isPinyin(cityBean.getPinyin().charAt(0)).equals(list.get(i).getName())) {
                    if (list.get(i).getCity() == null) {
                        List<FriendCityBean> cityBeanList = new ArrayList<>();
                        list.get(i).setCity(cityBeanList);
                    }
                    list.get(i).getCity().add(cityBean);
                }
            }
        }

        return list;
    }
    static public List<FriendBean> noSortFriend(List<MembersBean> membersBeans) {

        List<FriendCityBean> data = new ArrayList<>();
        for (MembersBean membersBean : membersBeans) {
            if (membersBean.getMember_id() == UserInfoUtil.getUserId()) {
                continue;
            }
            data.add(FriendCityBean.convert(membersBean));
        }

        // 先设置每个用户的拼音，然后根据拼音排序
        for (FriendCityBean bean : data) {
            String pinyin = Pinyin.toPinyin(EmptyUtils.isNotEmpty(bean.getRemarks_name()) ? bean.getRemarks_name() : bean.getNickname(), "");
            bean.setPinyin(pinyin.toUpperCase());
        }

        // 排序成功后，整合数据
        // 先添加字幕列表数据
        List<FriendBean> list = new ArrayList<>();
        for (FriendCityBean bean : data) {
            boolean isAdd = true;
            for (FriendBean subBean : list) {
                if (subBean.getName().equals(PinyinUtil.isPinyin(bean.getPinyin().charAt(0)))) {
                    isAdd = false;
                    break;
                }
            }
            if (isAdd) {
                FriendBean friendBean = new FriendBean();
                friendBean.setName(PinyinUtil.isPinyin(bean.getPinyin().charAt(0)));
                List<FriendCityBean> cityBeanList = new ArrayList<>();
                friendBean.setCity(cityBeanList);
                list.add(friendBean);
            }
        }
        // 在添加好友数据
        for (FriendCityBean cityBean : data) {
            for (int i = 0; i < list.size(); i++) {
                if (PinyinUtil.isPinyin(cityBean.getPinyin().charAt(0)).equals(list.get(i).getName())) {
                    if (list.get(i).getCity() == null) {
                        List<FriendCityBean> cityBeanList = new ArrayList<>();
                        list.get(i).setCity(cityBeanList);
                    }
                    list.get(i).getCity().add(cityBean);
                }
            }
        }

        return list;
    }

    static public List<FriendBean> sortFriendFilter(List<MembersBean> membersBeans, List<FriendBean> friendBeans) {

        List<FriendCityBean> data = new ArrayList<>();
        for (MembersBean membersBean : membersBeans) {
            if (membersBean.getMember_id() == UserInfoUtil.getUserId()) {
                continue;
            }
            data.add(FriendCityBean.convert(membersBean));
        }

        // 先设置每个用户的拼音，然后根据拼音排序
        for (FriendCityBean bean : data) {
            String pinyin = Pinyin.toPinyin(EmptyUtils.isNotEmpty(bean.getRemarks_name()) ? bean.getRemarks_name() : bean.getNickname(), "");
            bean.setPinyin(pinyin.toUpperCase());
        }
        // 排序
        PinyinUtil.transferList(data);
        // 排序成功后，整合数据
        // 先添加字幕列表数据
        List<FriendBean> list = new ArrayList<>();
        for (FriendCityBean bean : data) {
            boolean isAdd = true;
            for (FriendBean subBean : list) {
                if (subBean.getName().equals(PinyinUtil.isPinyin(bean.getPinyin().charAt(0)))) {
                    isAdd = false;
                    break;
                }
            }
            if (isAdd) {
                FriendBean friendBean = new FriendBean();
                friendBean.setName(PinyinUtil.isPinyin(bean.getPinyin().charAt(0)));
                List<FriendCityBean> cityBeanList = new ArrayList<>();
                friendBean.setCity(cityBeanList);
                list.add(friendBean);
            }
        }
        // 在添加好友数据
        for (FriendCityBean cityBean : data) {
            for (int i = 0; i < list.size(); i++) {
                if (PinyinUtil.isPinyin(cityBean.getPinyin().charAt(0)).equals(list.get(i).getName())) {
                    if (list.get(i).getCity() == null) {
                        List<FriendCityBean> cityBeanList = new ArrayList<>();
                        list.get(i).setCity(cityBeanList);
                    }
                    list.get(i).getCity().add(cityBean);
                }
            }
        }

        return list;
    }

}
